package com.shenjialin.Map;


import com.shenjialin.Game.Explode;
import com.shenjialin.Game.GameFrame;
import com.shenjialin.Tank.Tank;
import com.shenjialin.util.Constant;
import com.shenjialin.util.MapTilePool;
import com.shenjialin.util.MyUtil;

import java.awt.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.List;


/**
 * 游戏地图类
 */
public class GameMap {
    // 地图属性
    public static String data;
    public static String[] dataArray;
    protected static Path path;
    static {
        path = Paths.get("E:/tanke/Map_file/Map0.txt");
        try {
            data = Files.readString(path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        dataArray = data.split("\r\n");
    }
    //public static final int MAP_X = Tank.RADIUS*3;
    public static final int MAP_X = 0;
    //public static final int MAP_Y = Tank.RADIUS*3 + GameFrame.titleBarH;
    public static final int MAP_Y = GameFrame.titleBarH;

    //public static final int MAP_WIDTH = Constant.FRAME_WIDTH - Tank.RADIUS*6;
    public static final int MAP_WIDTH = Constant.FRAME_WIDTH;
    //public static final int MAP_HEIGHT = Constant.FRAME_HEIGHT - Tank.RADIUS*8 - GameFrame.titleBarH;
    public static final int MAP_HEIGHT = Constant.FRAME_HEIGHT - GameFrame.titleBarH;
    // 地图元素块的容器
    private List<MapTile> tiles = new ArrayList<>();



    public GameMap() {
        try {
            initMap();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 初始化地图元素块
     */
    private void initMap() throws IOException {
        System.out.println(tiles.size());
        for (int i = 0; i < dataArray.length; i ++) {
            for (int j = 0; j < dataArray[0].length(); j ++) {
                    if(dataArray[i].charAt(j) == '1') {
                        MapTile mapTile= MapTilePool.get();
                        mapTile.setTileType(MapTile.TILE_TYPE1);
                        mapTile.setVisible(true);
                        mapTile.setX(j*MapTile.tileW + MAP_X);
                        mapTile.setY(i*MapTile.tileW + MAP_Y);
                        mapTile.setName(null);
                        tiles.add(mapTile);
                    }else if(dataArray[i].charAt(j) == '2') {
                        MapTile mapTile= MapTilePool.get();
                        mapTile.setTileType(MapTile.TILE_TYPE2);
                        mapTile.setVisible(true);
                        mapTile.setX(j*MapTile.tileW + MAP_X);
                        mapTile.setY(i*MapTile.tileW + MAP_Y);
                        mapTile.setName(null);
                        tiles.add(mapTile);
                    }
            }
        }
        System.out.println(tiles.size());
//                        if(Math.random() > Constant.TILE_PERCENT) {
//        MapTile tile = MapTilePool.get();
//        tile.setVisible(true);
//        tile.setX(i);
//        tile.setY(j);
//        tile.setName(null);
//        tiles.add(tile);

        house = new TankHouse();
        for (MapTile tile : house.getTileList()) {
            tiles.add(tile);
        }

    }
    private TankHouse house;

    public void draw(Graphics g) {
//        for (MapTile tile : tiles) {
//            tile.draw(g);
//        }
        for (int i = 0; i < tiles.size(); i++) {
            tiles.get(i).draw(g);
        }
    }

    public List<MapTile> getTiles() {
        return tiles;
    }

    /**
     * 将所有不可见的地图块从容器中移除
     */
    public void clearDestoryTile() {
        for (int i = 0; i < tiles.size(); i++) {
            MapTile tile = tiles.get(i);
            if(!tile.isVisible()) {
                tiles.remove(i);
            }
        }
    }

    public void tilesReturn() {
        for (MapTile tile : tiles) {
            MapTilePool.theReturn(tile);
        }
        tiles.clear();
    }
}
